package nju.firework.scene;
import java.util.Iterator;

import nju.firework.game.IO.GameReader;
import nju.firework.game.main.MainActivity;

public class Map{
	/**
	 * @author WangXuanyi
	 * @since 2012.10.24
	 */
	
	//managers
	
	private MapElement[] map;
	private Queue currentMap;
	
	public Map(MainActivity mainActivity){
		// init map
		map = GameReader.readMap(mainActivity); 
		// init currentMap
		currentMap = new Queue(MapConst.currentCells);
		// init cellManager
		// init locations
		initLocations();
	}
	
	public MapElement[] getCurrentMap(){
			MapElement[] tmpMap = new MapElement[MapConst.currentCells];
			QueueIterator itr = (QueueIterator) currentMap.iterator();
			int k = 0;
			while(itr.hasNext()){
				tmpMap[k] = itr.next();
				k++;
			}
		return tmpMap;
	}
	
	public void advance(){
		currentMap.update();
		return;
	}
	
	public void backward(){
		currentMap.backward();
	}
	
	public MapElement getRear(){
		return currentMap.getRear();
	}
	
	public boolean isEnd(){
		return currentMap.isEnd();
	}
	
	public boolean isStart(){
		return currentMap.isStart();
	}
	
	//private methods
	private void initLocations(){
	}	
	

	
	//inner classes
 	private class Queue implements Iterable<MapElement>{
		int head;
		int rear;
		
		public Queue(int length){
			head = 0;
			rear = length - 1;
		}
		
		void update(){
			head++;
			rear++;
		}
		
		void backward(){
			head--;
			rear--;
		}
		
		boolean isStart(){
			if(head<0){
				return true;
			}
			else
				return false;
		}
		
		boolean isEnd(){
			if(rear>map.length){
				return true;
			}
			else{
				return false;
			}
				
		}
		
		MapElement getRear(){
			return map[rear];
		}

		@Override
		public Iterator<MapElement> iterator() {
			// TODO Auto-generated method stub
			return new QueueIterator(this);
		}
		
	}

	private class QueueIterator implements Iterator<MapElement>{
		Queue mQueue;
		int position;
	
		public QueueIterator(Queue pQueue){
			mQueue = pQueue;
			position = -1;
		}
		
		@Override
		public boolean hasNext() {
			// TODO Auto-generated method stub
			if(mQueue.head + position == mQueue.rear)
				return false;
			else 
				return true;
		}

		@Override
		public MapElement next() {
			// TODO Auto-generated method stub
			position++;
			MapElement e = map[mQueue.head+position];
			return e;
		}

		@Override
		public void remove() {
			// TODO Auto-generated method stub
			
		}
		
	}
	
}
